package com.pengke.pangu.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pengke.pangu.annotation.SLog;
import com.pengke.pangu.common.enums.BizType;
import com.pengke.pangu.common.utils.R;
import com.pengke.pangu.service.sys.entity.SysPermission;
import com.pengke.pangu.service.sys.service.ISysPermissionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {

    @Resource
    private ISysPermissionService sysPermissionService;

    @GetMapping("/getSysPermissionList")
    @SaCheckPermission("sys:permission:read")
    @SLog(model="权限管理", bizType = BizType.QUERY)
    public R getSysPermissionTreeList(SysPermission sysPermission){
        List<SysPermission> list = sysPermissionService.list(Wrappers.<SysPermission>query().lambda()
                .eq(ObjectUtil.isNotEmpty(sysPermission.getTitle()), SysPermission::getTitle, sysPermission.getTitle())
        );

        TreeNodeConfig config = new TreeNodeConfig();
        config.setParentIdKey("pid");
        config.setNameKey("title");
        config.setChildrenKey("children");
        config.setWeightKey("orderId");

        List<Tree<Object>> treeList = TreeUtil.build(list, 0,config,(obj, treeNode)->{
            treeNode.putExtra("id",obj.getId());
            treeNode.putExtra("pid",obj.getPid());
            treeNode.putExtra("title",obj.getTitle());
            treeNode.putExtra("permissionCode",obj.getPermissionCode());
            treeNode.putExtra("type",obj.getType());
            treeNode.putExtra("path",obj.getPath());
            treeNode.putExtra("componentName",obj.getComponentName());
            treeNode.putExtra("componentPath",obj.getComponentPath());
            treeNode.putExtra("hidden",obj.getHidden());
            treeNode.putExtra("disable",obj.getDisable());
            treeNode.putExtra("orderId",obj.getOrderId());
            treeNode.putExtra("icon",obj.getIcon());
            treeNode.putExtra("keepAlive",obj.getKeepAlive());
        });
        return R.ok(treeList);
    }

    @PostMapping("/createSysPermission")
    @SaCheckPermission("sys:permission:create")
    @SLog(model="权限管理", bizType = BizType.INSERT)
    public R createPermission(@RequestBody SysPermission sysPermission){
        Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
        sysPermission.setCreateUser(userId);
        sysPermissionService.save(sysPermission);
        return R.ok();
    }

    @PostMapping("/updateSysPermissionById")
    @SaCheckPermission("sys:permission:update")
    @SLog(model="权限管理", bizType = BizType.UPDATE)
    public R updateSysPermissionById(@RequestBody SysPermission sysPermission){
        Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
        sysPermission.setUpdateUser(userId);
        //入库和更新时间用数据库时间戳自动处理
        sysPermission.setCreateTime(null);
        sysPermission.setUpdateTime(null);
        sysPermissionService.updateById(sysPermission);
        return R.ok();
    }
}
